package com.hhit.collaborative_learning.management;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SendEmailServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 收件人的电子邮件 ID
		String to = request.getParameter("to_email");

		// 发件人的电子邮件 ID
		String from = "collaborative_hhit@163.com";

		// 假设您是从本地主机发送电子邮件
		String host = "smtp.163.com";

		// 获取系统的属性
		Properties properties = System.getProperties();

		// 设置邮件服务器
		properties.setProperty("mail.smtp.host", host);
		
		properties.setProperty("mail.smtp.auth", "true");

		// 获取默认的 Session 对象
		MyAuthenticator myauth = new MyAuthenticator("collaborative_hhit@163.com", "930611");
		Session session = Session.getDefaultInstance(properties,myauth);

		// 设置响应内容类型
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		//生成验证码
		String code=getCode();

		try {
			// 创建一个默认的 MimeMessage 对象
			MimeMessage message = new MimeMessage(session);
			// 设置 From: header field of the header.
			message.setFrom(new InternetAddress(from));
			// 设置 To: header field of the header.
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					to));
			// 设置 Subject: header field
			message.setSubject("协作学习系统");
			// 现在设置实际消息
			message.setText("欢迎您使用协作学习系统，验证码为"+code);
			// 发送消息
			Transport.send(message);
			
			out.print(code);
//			System.out.println("code="+code);
//			String title = "发送电子邮件";
//			String res = "成功发送消息...";
//			String docType = "<!doctype html public \"-//w3c//dtd html 4.0 "
//					+ "transitional//en\">\n";
//			out.println(docType + "<html>\n" + "<head><title>" + title
//					+ "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n"
//					+ "<h1 align=\"center\">" + title + "</h1>\n"
//					+ "<p align=\"center\">" + res + "</p>\n"
//					+ "</body></html>");
		} catch (MessagingException mex) {
			mex.printStackTrace();
		}
	}

	class MyAuthenticator extends javax.mail.Authenticator {
		private String strUser;
		private String strPwd;

		public MyAuthenticator(String user, String password) {
			this.strUser = user;
			this.strPwd = password;
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(strUser, strPwd);
		}
	}
	
	public String getCode(){
		StringBuilder sb=new StringBuilder("");
		for(int i=0;i<6;i++){
			sb.append((int)(Math.random()*10));
		}
		return sb.toString();
	}

}
